package codegen

import (
	"gitee.com/hxchjm/goge/codegen/utils"
	"github.com/siddontang/go/config"
)

const cfgTemplate = `
app:
  demoexpire: 2s

redis:
  name: "{{.serviceName}}"
  proto: "tcp"
  addr: "127.0.0.1:6379"
  idle: 10
  active: 10
  dialTimeout: 1s
  readTimeout: 1s
  writeTimeout: 1s
  idleTimeout: 10s

grpc:
  addr: 0.0.0.0:9000
  timeout: 1s

http:
  addr: 0.0.0.0:8000
  timeout: 1s

db:
  addr: 127.0.0.1:3306
  dsn: "{user}:{password}@tcp(127.0.0.1:3306)/{database}?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8mb4,utf8"
  readDSN: ["{user}:{password}@tcp(127.0.0.2:3306)/{database}?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8mb4,utf8","{user}:{password}@tcp(127.0.0.3:3306)/{database}?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8,utf8mb4"]
  active: 20
  idle: 10
  idleTimeout: 4h
  queryTimeout: 200ms
  execTimeout:  300ms
  tranTimeout:  400ms
`

func genConfig(dir, rootPkg string, cfg *config.Config, filename string) (err error) {

	if err = utils.GenFile(utils.FileGenConfig{
		Dir:             dir,
		Subdir:          configsDir,
		Filename:        filename,
		BuiltinTemplate: cfgTemplate,
		Data: map[string]string{
			"serviceName": rootPkg,
		},
	}); err != nil {
		return err
	}

	return
}
